package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Objects;
import com.hitqz.robot.api.business.dto.RobotEquipmentControlButtonDto;
import com.hitqz.robot.api.business.dto.RobotEquipmentPaneButtonsDto;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.common.entity.Button;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.mapper.RobotEquipmentMapper;
import com.hitqz.robot.biz.service.ButtonService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@AllArgsConstructor
public class RobotEquipmentServiceIml extends ServiceImpl<RobotEquipmentMapper, RobotEquipment> implements RobotEquipmentService {

    final private ButtonService buttonService;

    final private EquipmentFactory equipmentFactory;

    final private RobotDriverService robotDriverService;

    @Override
    public List<RobotEquipmentPaneButtonsDto> getControlButtonList() {
        List<RobotEquipmentPaneButtonsDto> panels = new ArrayList<>();
        List<RobotEquipment> list = this.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getShowPanel, 1));
        for (RobotEquipment robotEquipment : CollUtil.emptyIfNull(list)) {
            Integer type = robotEquipment.getType();
            List<RobotEquipmentControlButtonDto> buttons = new ArrayList<>();
            //根据设备类型获取按钮
            EquipmentEnum[] values = EquipmentEnum.values();
            for (EquipmentEnum value : values) {
                if (Objects.equal(value.getType(), type)){
                    List<Button> buttonDb = buttonService.list(Wrappers.<Button>lambdaQuery().eq(Button::getObject, value.getDes()).eq(Button::getIsShow, true));
                    List<RobotEquipmentControlButtonDto> dtos = BeanUtil.copyToList(buttonDb, RobotEquipmentControlButtonDto.class);
                    if (CollUtil.isNotEmpty(dtos)) {
                        //设置参数
                        dtos.forEach(b->b.setParam(JSON.toJSONString(robotEquipment)));
                        buttons.addAll(dtos);
                    }
                    break;
                }
            }
            if(!buttons.isEmpty()){
                RobotEquipmentPaneButtonsDto panel = new RobotEquipmentPaneButtonsDto();
                panel.setTitle(robotEquipment.getName()+"操作面板");
                panel.setButtons(buttons);
                panels.add(panel);
            }
        }
        return panels;
    }

    @Override
    public List<EquipmentParamsDto> getEquipmentParams(RobotEquipment equipment) {
        List<EquipmentParamsDto> params = new ArrayList<>();
        try {
            if (Objects.equal(equipment.getBrand(), BrandEnum.ROBOT.name())) {
                //通过机器人驱动控制的外设
                Integer type = equipment.getType();
                if (Objects.equal(type, EquipmentEnum.ROBOT.getType())) {
                    R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS, null);
                    params = result.getData() == null ? params : (List<EquipmentParamsDto>) result.getData();
                }
            } else {
                //真的外设
                IEquipmentService service = equipmentFactory.getServiceByBrand(equipment.getBrand());
                QueryParamsDto queryParamsDto = new QueryParamsDto();
                queryParamsDto.setUsername(equipment.getUsername());
                queryParamsDto.setPort(equipment.getPort());
                queryParamsDto.setPassword(equipment.getPassword());
                queryParamsDto.setIp(equipment.getIp());
                queryParamsDto.setChannel(equipment.getChannel());
                params = service.getEquipmentParams(queryParamsDto);
            }
        } catch (Exception e) {
            log.error("获取外设参数失败", e);
        }
        return params;
    }


    private RobotEquipmentControlButtonDto createButton(String name, String action, String param, String picUrl, String picUrlP, String picUrlS,boolean isLongClick) {
        RobotEquipmentControlButtonDto button = new RobotEquipmentControlButtonDto();
        button.setName(name);
        button.setAction(action);
        button.setLongClick(isLongClick);
        button.setPostType("post");
        button.setParam(param);
        button.setDataType("json");
        button.setShowBtnPicUrl(picUrl);
        button.setClickedBtnPicUrl(picUrlP);
        button.setIconNotAllow(picUrlS);
        button.setMapAction("");
        return button;
    }
}
